/*
 	utility.cc

 	Copyright (c) 2008 Bojan Rajkovic

 	Permission is hereby granted, free of charge, to any person obtaining a copy
 	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
 	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 	copies of the Software, and to permit persons to whom the Software is
 	furnished to do so, subject to the following conditions:

 	The above copyright notice and this permission notice shall be included in
 	all copies or substantial portions of the Software.

 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 	THE SOFTWARE.
*/

#include "../include/utility.h"

using namespace std;

// Get the current date and time from ctime, and get rid
// of the annoying newline that ctime returns.
char* utility::get_current_date_time() {

	// Get the current time
	time_t current_time;
	time(&current_time);

	// Format it with ctime, and get rid of that damn newline
	char* time_string = ctime(&current_time);
	// Trim that annoying newline out of ctime
	if (time_string[strlen(time_string)-1] == '\n') {
		time_string[strlen(time_string)-1] = 0;
	}

	// Spit the formatted time back out
	return time_string;

}

// Push a message to the log, specifying the message type as well
void utility::log_message(const char* message, int message_type) {
	
	int priority;
	
	switch (message_type) {
		case M_ERROR:
			priority = LOG_ERR | LOG_DAEMON;
			break;
		case M_INFO:
			priority = LOG_NOTICE | LOG_DAEMON;
			break;
		default:
			priority = LOG_INFO | LOG_DAEMON;
			break;
	}
	
	openlog("keycounter", LOG_PID, LOG_DAEMON);
	syslog(priority, "%s", message);
	closelog();
}

// Check if the PID inside the specified file is valid (ie. if there is such a process)
// We'll assume that the PID is on the first line of the file
// since that's the format we're writing it in.
bool utility::check_pid_file(const char* pidfile) {
	ifstream read_pid (pidfile);
	if (!read_pid.good()) {
		cerr << _("Could not open PID file for reading, maybe the daemon isn't running as root?") << endl;
		exit(EXIT_FAILURE);
	}
	string line;
	getline(read_pid, line);
	read_pid.close();
	if (line == "") {
		// Can't be valid if it's blank
		return false;
	} else {
		pid_t old_pid = (pid_t)atoi(line.c_str());
		if (kill(old_pid,0) == 0) { // we just want to know if it exists, we don't want to send it a signal
			// it exists, so it's valid
			return true;
		} else {
			// nope, not valid, since it's not running
			// must've died badly
			return false;
		}
	}
}

char* utility::sha1(const char* string) {
	
	unsigned char message_digest[20];
	SHA1((unsigned char*)string, strlen(string), message_digest);

	char *out = (char *) malloc(41);
	
	for ( int i = 0; i < 20; i++, out += 2 ) {
		snprintf ( out, 3, "%02x", message_digest[i] );
	}

	return out;
}
